﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EasySolutionForAll.Models;
using EasySolutionForAll.Models.Quiz;

namespace EasySolutionForAll.Controllers
{
    public class PublicController : Controller
    {
        private ApplicationDbContext db = new ApplicationDbContext();
        // GET: Public
        public ActionResult Index()
        {
            return View(db.Categories.ToList());
        }

        public ActionResult RandomQuestion()
        {
            QuestionSet questionSet = null;
            int index = new Random().Next(1, db.QuestionSet.Count());
            if (db.QuestionSet.Any())
            {
                questionSet = db.QuestionSet.First(c => c.QuestionSetId == index);
            }
            return View(questionSet);
            
        }

        [HttpPost]
        public JsonResult CheckAnswer(int questionSetId, int answerId)
        {
            var questionSet = db.QuestionSet.First(c => c.QuestionSetId == questionSetId);
            var result = questionSet.Answers.First(c => c.AnswerId == answerId).IsCorrect;
            return new JsonResult()
            {
                Data = new { result = result }, 
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }
    }
}